<?php
  // scoreboard log
  header("Content-Type: text/plain; charset=UTF-8");
  $max = 20;
  // get input
  $name = htmlspecialchars(strip_tags(substr(@$_REQUEST['name'],0,20))); 
  $name = str_replace("\n"," ",$name);
  $name = str_replace("\r"," ",$name);
  $name = str_replace("\t"," ",$name);
  if (empty($name))
    $name = 'anonym';
  $life = 1*htmlspecialchars(strip_tags(substr(@$_REQUEST['life'],0,1200)));
  $level = 1*htmlspecialchars(strip_tags(substr(@$_REQUEST['level'],0,1200)));
  $kills = 1*htmlspecialchars(strip_tags(substr(@$_REQUEST['kills'],0,1200)));
  $aliens = 1*htmlspecialchars(strip_tags(substr(@$_REQUEST['aliens'],0,1200)));
  $credit = 1*htmlspecialchars(strip_tags(substr(@$_REQUEST['credit'],0,1200)));
  // load previous score
  $f = file_get_contents("score.txt");
  // limit total length of chat
  $f = explode("\n",$f,$max+1);
  //echo count($f)."\n\n";
  while (count($f) > $max)
    array_pop($f);
  //for ($i=0; $i<count($f); $i++) 
  //  echo "i=$i ".$f[$i]."-----\n\n";
  $z = implode("\n",$f);
  $s = '';
  // undefined name
  if ( (!empty($name))&&($level>0) ) {  
    // insert new score
    if ($life > 0)
      $s = "<div class=\"post\"><span class=\"name\">".$name."</span><span class=\"message\">has entered level $level, total $kills kills, credit $credit, life $life</span></div>\n";  
    else
      $s = "<div class=\"post\"><span class=\"name\">".$name."</span><span class=\"message\">has died in level $level, total $kills kills, $aliens aliens remained, credit $credit</span></div>\n";  
    $z = $s.$z;
    // write file
    file_put_contents("score.txt",$z);
  }  
  echo $z;
?>